package com.sskj.mine.ui.activity;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.LazyLoaders;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.box.verifier.ToastNextInputs;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.SetLoginPwdPresenter;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/01
 */
@Route(path = RConfig.MINE_SET_LOGIN_PWD)//设置登录密码
public class SetLoginPwdActivity extends BaseActivity<SetLoginPwdPresenter> {
    @BindView(R2.id.password_edt)
    EditText passwordEdt;
    @BindView(R2.id.old_pwd)
    EditText oldPwd;
    @BindView(R2.id.password_edt2)
    EditText passwordEdt2;
    @BindView(R2.id.submit_btn)
    Button submitBtn;
    @Inject
    UserViewModel userViewModel;
    String mobile;
    String hint;
    String hint2;
    @BindView(R2.id.iv_old_pwd)
    ImageView ivOldPwd;
    @BindView(R2.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R2.id.ivPwdToggle2)
    ImageView ivPwdToggle2;
    private UserData userData;
    private boolean isSee;
    private boolean isSee2;
    private boolean isSee3;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_setting_password;
    }

    @Override
    public SetLoginPwdPresenter getPresenter() {
        return new SetLoginPwdPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);

        ARouter.getInstance().inject(this);

        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                mobile = userData.getTel();
            } else {
                userData = null;
            }
        });

        setTitle(App.INSTANCE.getString(R.string.minesetLoginPwdActivity1));


        ToastNextInputs inputs = new ToastNextInputs();
        LazyLoaders lazyLoaders = new LazyLoaders(this);
        inputs
                .add(oldPwd, SchemeUtil.notEmpty(oldPwd), SchemeUtil.pwd())
                .add(passwordEdt, SchemeUtil.notEmpty(passwordEdt), SchemeUtil.pwd())
                .add(passwordEdt2, SchemeUtil.notEmpty(passwordEdt), SchemeUtil.equalsPwd(lazyLoaders.fromEditText(passwordEdt)));
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initEvent() {


        ClickUtil.click(200, ivOldPwd, () -> {
            isSee3 = !isSee3;
            oldPwd.setTransformationMethod(isSee3 ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivOldPwd.setImageResource(isSee3 ? R.mipmap.mine_icon_show : R.mipmap.mine_icon_hide);

        });
        ClickUtil.click(200, ivPwdToggle, () -> {
            isSee = !isSee;
            passwordEdt.setTransformationMethod(isSee ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggle.setImageResource(isSee ? R.mipmap.mine_icon_show : R.mipmap.mine_icon_hide);

        });

        ClickUtil.click(200, ivPwdToggle2, () -> {
            isSee2 = !isSee2;
            passwordEdt2.setTransformationMethod(isSee2 ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggle2.setImageResource(isSee2 ? R.mipmap.mine_icon_show : R.mipmap.mine_icon_hide);

        });

        ClickUtil.click(submitBtn, () -> {

            if (isEmpty(oldPwd)) {
                ToastUtil.showShort(oldPwd.getHint());
                return;
            }

            if (isEmpty(passwordEdt)) {
                ToastUtil.showShort(passwordEdt.getHint());
                return;
            }
            if (isEmpty(passwordEdt2)) {
                ToastUtil.showShort(passwordEdt2.getHint());
                return;
            }
            if (!CheckUtil.isPayPwd(getText(passwordEdt))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minesetLoginPwdActivity2));
                return;
            }
            if (!getText(passwordEdt).equals(getText(passwordEdt2))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minesetLoginPwdActivity3));
                return;
            }
            mPresenter.setLoginPwd(passwordEdt.getText().toString(), oldPwd.getText().toString());

        });
    }


    public void updataUI() {

        LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
        logoutProvider.logout();

    }

}
